# encoding=utf-8
from spider.book_category_process import get_book_pagenum, get_six_book_from_one_category, get_book_list
from spider.base import get_category
from concurrent.futures import ThreadPoolExecutor, as_completed
import time
from storages.cache import cache
from constant.const import const

base_url = 'https://www.bbiquge.net/'

header = {
    "Accept-Encoding": "gzip, deflate",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Cookie": "HMACCOUNT_BFESS=07B85514E05D22AE; H_WISE_SIDS_BFESS=110085_127969_179349_180636_185638_188749_189755_191527_194085_194519_196428_196527_197471_197711_197948_199572_203518_204916_207236_207697_208721_209568_210321_210732_210757_211953_212295_212726_212739_212797_212867_212912_213039_213059_213094_213289_213357_213484_214094_214205_214379_214596_214652_214791_214884_215070_215127_215280_215333_215554_215730_215859_215892_216043_216253_216297_216335_216342_216353_216368_216446_216596_216631_216634_216645_216843_217183_217320_217410_217439_217453_217514_217760_217868; BAIDUID_BFESS=FD30747E577715F2704ED40BB1AE3016:FG=1; BDUSS_BFESS=VJSY3dVTEpVN34ycjJFUjRjanA0Qy00UWR5allmVWFkTVE0enI3a21iUDduU05rRVFBQUFBJCQAAAAAAAAAAAEAAABJszI0xfS71L3wxaMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPsQ~GP7EPxjO; ZFY=1S3:AvULhrXM1QTkZp:AmNtEmf:AUoPgmjyb8voM0q2j:BE:C",
    "Host": "hm.baidu.com",
    "Referer": "https://www.bbiquge.net/",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36",
}

# category_name = const.quanben
category_name = const.fenlei + str(5)
c = cache()
get = str(c.get(const.category, category_name, const.map), encoding="utf-8")
if const.fenlei in category_name:
    get = get.replace("_1", "_{}")
print(get.format(str(1)))


def get_num():
    url = get
    if const.fenlei in category_name:
        url = url.format(str(1))
    pagenum = get_book_pagenum(url, header)
    return int(pagenum)


# print(get_num())

def test(num):
    url = get
    if const.fenlei in category_name:
        url = url.format(str(num + 1))
    get_book_list(category_name, url, header)


with ThreadPoolExecutor(max_workers=10) as t:
    obj_list = []
    begin = time.time()
    for page in range(int(get_num())):
        obj = t.submit(test, page)
        obj_list.append(obj)
    for future in as_completed(obj_list):
        future.result()
    times = time.time() - begin
    print(times)
